package stack;

import array.Array;

public class ArrayStack <E> implements Stack<E>{
	private Array<E> array;

	public ArrayStack (int capacity) {
		array = new Array<E>(capacity);
	}
	
	public ArrayStack() {
		array = new Array<E>();
	}

	@Override
	public int getSize() {
		return array.getSize();
	}

	@Override
	public boolean isEmpty() {
		return array.isEmpty();
	}

	@Override
	public void push(E e) {
		//像栈中压入元素
		array.addLast(e);
	}

	@Override
	public E pop() {
		//移除并取出栈顶的元素
		return array.removeLast();
	}

	@Override
	public E peek() {
		//去除栈顶的元素
		return array.getLast();
	}

	@Override
	public String toString() {
		StringBuilder res = new StringBuilder();
		res.append("Stack:");
		res.append("[");
		for(int i = 0 ; i < getSize() ; i++) {
			res.append(array.get(i));
			if(i < (getSize()-1)) {
				res.append(',');
			}
		}
		res.append("]top");
		return res.toString();
	}
	
	public static void main(String[] args) {
		ArrayStack<Integer> stack = new ArrayStack<>();
		for(int i = 0 ; i < 10 ; i++) {
			stack.push(i);
		}
		stack.pop();
		stack.push(52);
		System.out.println(stack);
	}
}
